﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Database;

namespace ConnectDatabase
{
    public class RulesInfoDB
    {
        DatabaseDataContext dbContext = new DatabaseDataContext();

        #region Các hàm cơ bản
        public List<tblRulesInfo> GetAll()
        {
            try
            {
                var list = from a in dbContext.tblRulesInfos select a;
                return list.ToList<tblRulesInfo>();
            }
            catch (Exception ex)
            {
                return new List<tblRulesInfo>();
            }
        }

        public tblRulesInfo GetByKey(int id)
        {
            try
            {
                var obj = dbContext.tblRulesInfos.SingleOrDefault(a => a.Id == id);
                return obj;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public int Insert(tblRulesInfo obj)
        {
            try
            {
                dbContext.tblRulesInfos.InsertOnSubmit(obj);
                dbContext.SubmitChanges();
                return obj.Id;
            }
            catch (Exception ex)
            {                
                return -1;
            }
        }

        public bool Update(tblRulesInfo obj)
        {
            bool isOk = false;
            try
            {
                var exist = dbContext.tblRulesInfos.SingleOrDefault(a => a.Id == obj.Id);
                if (exist != null)
                {
                    dbContext.SubmitChanges();
                    isOk = true;
                }
            }
            catch (Exception ex)
            {
                isOk = false;
            }
            return isOk;
        }
        #endregion
    }
}
